package ec.genetic.main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import ec.genetic.impl.Evolution;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EquationGenSolverWindow extends JFrame {

	private JPanel contentPane;
	private JTextField a_value;
	private JTextField populationSize;
	private JTextField accuracy;
	private JTextField b_value;
	private JTextField l_bound;
	private JTextField r_bound;
	private JTextField maxIteration;
	private JTextField startFitness;
	private JTextField finalFitness;
	private JTextField resultIterations;
	private JTextField result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EquationGenSolverWindow frame = new EquationGenSolverWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EquationGenSolverWindow() {
		setTitle("Linear Equation Genetic Solver");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 432, 322);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JButton solveButton = new JButton("Solve");
		solveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int populSize = Integer.parseInt(populationSize.getText());
				double a = Double.parseDouble(a_value.getText());
				double b = Double.parseDouble(b_value.getText());
				
				Evolution evolution = new Evolution(a, b, populSize);			
				
				//Accuracy
				evolution.setAccuracy(Double.parseDouble(accuracy.getText()));
				
				//Max number of iterations
				evolution.setIterations(Integer.parseInt(maxIteration.getText()));
				
				//Left and right bounds
				evolution.setLeftBound(Double.parseDouble(l_bound.getText()));
				evolution.setRightBound(Double.parseDouble(r_bound.getText()));
				
				//Run genetic algorithm!
				evolution.letEvolutionSolveIt();
				
				// Make output
				
				//Start fitness
				startFitness.setText(String.valueOf(evolution.getStartFitness()));
				
				//Final fitness
				finalFitness.setText(String.valueOf(evolution.getFinishFitness()));
				
				//Number of iterations
				if(evolution.getIteration() == evolution.getIterations()){
					resultIterations.setText("All");
				}else{
					resultIterations.setText(String.valueOf(evolution.getIteration()));
				}
				
				//Set result
				result.setText(String.valueOf(evolution.getAnswer()));
			}
		});
		solveButton.setBounds(42, 244, 118, 31);
		panel.add(solveButton);
		
		a_value = new JTextField();
		a_value.setText("4");
		a_value.setBounds(10, 54, 33, 20);
		panel.add(a_value);
		a_value.setColumns(10);
		
		populationSize = new JTextField();
		populationSize.setText("50");
		populationSize.setColumns(10);
		populationSize.setBounds(160, 85, 35, 20);
		panel.add(populationSize);
		
		accuracy = new JTextField();
		accuracy.setText("0.1");
		accuracy.setColumns(10);
		accuracy.setBounds(160, 116, 35, 20);
		panel.add(accuracy);
		
		b_value = new JTextField();
		b_value.setText("-4");
		b_value.setColumns(10);
		b_value.setBounds(107, 54, 33, 20);
		panel.add(b_value);
		
		l_bound = new JTextField();
		l_bound.setText("-10");
		l_bound.setColumns(10);
		l_bound.setBounds(95, 190, 35, 20);
		panel.add(l_bound);
		
		r_bound = new JTextField();
		r_bound.setText("10");
		r_bound.setColumns(10);
		r_bound.setBounds(160, 190, 35, 20);
		panel.add(r_bound);
		
		JLabel lblNewLabel = new JLabel("*");
		lblNewLabel.setBounds(53, 57, 6, 17);
		panel.add(lblNewLabel);
		
		JLabel lblX = new JLabel("X");
		lblX.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblX.setBounds(69, 54, 22, 20);
		panel.add(lblX);
		
		JLabel label = new JLabel("+");
		label.setBounds(91, 57, 14, 17);
		panel.add(label);
		
		JLabel label_1 = new JLabel("=");
		label_1.setBounds(150, 57, 14, 17);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("0");
		label_2.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_2.setBounds(173, 54, 22, 20);
		panel.add(label_2);
		
		JLabel label_3 = new JLabel(" ..");
		label_3.setFont(new Font("Tahoma", Font.BOLD, 20));
		label_3.setBounds(134, 190, 18, 20);
		panel.add(label_3);
		
		JLabel lblPopulationSize = new JLabel("Population size");
		lblPopulationSize.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblPopulationSize.setBounds(10, 85, 140, 20);
		panel.add(lblPopulationSize);
		
		JLabel lblAccuracy = new JLabel("Accuracy");
		lblAccuracy.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblAccuracy.setBounds(10, 119, 81, 20);
		panel.add(lblAccuracy);
		
		JLabel lblInterval = new JLabel("Interval");
		lblInterval.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblInterval.setBounds(10, 190, 81, 20);
		panel.add(lblInterval);
		
		JLabel lblInput = new JLabel("Input");
		lblInput.setFont(new Font("Sylfaen", Font.BOLD | Font.ITALIC, 24));
		lblInput.setBounds(69, 11, 75, 31);
		panel.add(lblInput);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 41, 188, 2);
		panel.add(separator);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(10, 237, 188, 2);
		panel.add(separator_1);
		
		JLabel lblOutput = new JLabel("Output");
		lblOutput.setFont(new Font("Sylfaen", Font.BOLD | Font.ITALIC, 24));
		lblOutput.setBounds(245, 11, 102, 31);
		panel.add(lblOutput);
		
		JLabel lblStartFitness = new JLabel("Start fitness");
		lblStartFitness.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblStartFitness.setBounds(218, 82, 112, 20);
		panel.add(lblStartFitness);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setBounds(208, 41, 198, 2);
		panel.add(separator_2);
		
		JSeparator separator_3 = new JSeparator();
		separator_3.setBounds(208, 237, 198, 2);
		panel.add(separator_3);
		
		JLabel lblFinalFitness = new JLabel("Final fitness");
		lblFinalFitness.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblFinalFitness.setBounds(218, 131, 112, 20);
		panel.add(lblFinalFitness);
		
		JLabel lblNumOfIterations = new JLabel("Iteration");
		lblNumOfIterations.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblNumOfIterations.setBounds(217, 187, 92, 20);
		panel.add(lblNumOfIterations);
		
		JLabel lblResult = new JLabel("Result");
		lblResult.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblResult.setBounds(214, 246, 65, 20);
		panel.add(lblResult);
		
		JLabel lblIterations = new JLabel("Iterations");
		lblIterations.setFont(new Font("Tahoma", Font.BOLD, 17));
		lblIterations.setBounds(10, 150, 92, 20);
		panel.add(lblIterations);
		
		maxIteration = new JTextField();
		maxIteration.setText("5000");
		maxIteration.setColumns(10);
		maxIteration.setBounds(160, 147, 35, 20);
		panel.add(maxIteration);
		
		startFitness = new JTextField();
		startFitness.setEditable(false);
		startFitness.setColumns(10);
		startFitness.setBounds(331, 85, 75, 20);
		panel.add(startFitness);
		
		finalFitness = new JTextField();
		finalFitness.setEditable(false);
		finalFitness.setColumns(10);
		finalFitness.setBounds(331, 134, 75, 20);
		panel.add(finalFitness);
		
		resultIterations = new JTextField();
		resultIterations.setEditable(false);
		resultIterations.setColumns(10);
		resultIterations.setBounds(333, 190, 73, 20);
		panel.add(resultIterations);
		
		result = new JTextField();
		result.setEditable(false);
		result.setColumns(10);
		result.setBounds(295, 249, 80, 20);
		panel.add(result);
	}
}
